package com.coocaa.launcher.settings.appstoreupdate;

import android.os.RemoteException;

import com.coocaa.launcher.framework.manager.configdata.data.AppStoreNum;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.api.ContentApi;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_apptore.RP_New_Apps.AppStatNum;

public class AppStoreNews {
    private AppStoreBack mAppStoreBack = null;

    public interface AppStoreBack {
        public void onUpdateNum(AppStoreNum num);
    }

    public AppStoreNews() {

    }

    public void setAppStoreBack(AppStoreBack back) {
        mAppStoreBack = back;
    }

    public void getAppStoreUpdateNum() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                long curDataVersion = 0;
                try {
                    curDataVersion = XServiceManager.getLiteConfigService().getLongValue(
                            "appstoreversion", 0);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                AppStatNum data = ContentApi.getNewAppsNum();
                if (data != null && data.updateTime != null && data.count != null) {
                    long webDataVersion = Long.parseLong(data.updateTime);
                    if (webDataVersion > curDataVersion) {
                        AppStoreNum num = new AppStoreNum();
                        num.version = webDataVersion;
                        num.num = data.count;
                        LogUtils.d("num", "getNewApps   num:" + num.num + " num.version:" + num.version);
                        mAppStoreBack.onUpdateNum(num);
                    } else {
                        mAppStoreBack.onUpdateNum(null);
                    }

                } else {
                    mAppStoreBack.onUpdateNum(null);
                }
            }
        }).start();
    }

    public void saveNewAppsNum(long version) {
        try {
            XServiceManager.getLiteConfigService().saveLongConfig("appstoreversion", version);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
